/**
 * Created by jt on 2017/5/8.
 */
import "./index.less";
import projResultTpl from "./proj-result.handlebars";


jQuery(document).ready(function() {
    var projId = getArgs().id;
    var token = sessionStorage.getItem('supplier_token');
    var token_mng = sessionStorage.getItem('token');
    var matlist = [];
    var pubPriFlg = '0';  //不公开价格
    jQuery.ajax({
        url: HOST_ADDR + '/cpmp/xjProjMngForPchs/view/' + projId,
        type: 'get',
        dataType: 'json',
        async: false,
        contentType: "application/json",
        success: function (resp) {
            if (!resp.success) {
                alert(resp.msgCod + ' ' + resp.msgInf);
                return;
            }
            var projqotCntTyp = resp.data.xjProj.qotCntTyp == 2 ? '实时报价' : '非实时报价';
            var bidRngTyp = '非定向招标';
            var splrs = [];
            var currTyp = resp.data.xjProj.currTyp;
            var dpstFlg = resp.data.xjProj.dpstFlg == 1 ? '需要' : '不需要';
            var pubPriFlgFmt = resp.data.xjProj.pubPriFlg == 1 ? '是' : '否';
            pubPriFlg = resp.data.xjProj.pubPriFlg;
            if (resp.data.xjProj.bidRngTyp == 1) {
                bidRngTyp = '定向招标';
                for (var i = 0; i < resp.data.splrs.length; i++) {
                    splrs.push(resp.data.splrs[i].splrNam);
                }
            }
            if (resp.data.xjProj.currTyp == 'USD') {
                currTyp = '美元';
            } else if (resp.data.xjProj.currTyp == 'RMB') {
                currTyp = '人民币';
            }
            var rstPubTim = '', bidOpenTim = '', bidEndTim = '', projMemo = '', projNbr = '';
            if (resp.data.xjProj.rstPubTim != null && resp.data.xjProj.rstPubTim != '') {
                rstPubTim = (new Date(resp.data.xjProj.rstPubTim)).Format("yyyy-MM-dd hh:mm:ss");
            }
            if (resp.data.xjProj.bidOpenTim != null && resp.data.xjProj.bidOpenTim != '') {
                bidOpenTim = (new Date(resp.data.xjProj.bidOpenTim)).Format("yyyy-MM-dd hh:mm:ss");
            }
            if (resp.data.xjProj.bidEndTim != 'null' && resp.data.xjProj.bidEndTim != '') {
                bidEndTim = (new Date(resp.data.xjProj.bidEndTim)).Format("yyyy-MM-dd hh:mm:ss");
            }
            if (resp.data.xjProj.projMemo != null && resp.data.xjProj.projMemo != '') {
                var projMemo = resp.data.xjProj.projMemo;
            }
            if (resp.data.xjProj.projNbr != null && resp.data.xjProj.projNbr != '') {
                projNbr = resp.data.xjProj.projNbr;
            }
            // if(typeof(resp.data.xjProj.projSupv) == 'undefined' || resp.data.xjProj.projSupv == null){
            //     resp.data.xjProj.projSupv = '';
            // }
            // if(typeof(resp.data.xjProj.projSupvTel) == 'undefined' || resp.data.xjProj.projSupvTel == null){
            //     resp.data.xjProj.projSupvTel = '';
            // }

            console.log(resp);
            var matTyp = resp.data.xjProj.matTyp;
            if (resp.data.matTypDesc != null && resp.data.matTypDesc != '') {
                matTyp = resp.data.matTypDesc;
            }
            matlist = resp.data.matDtls;
            jQuery('.projTitle').html(resp.data.xjProj.projNam + '<div class="service-block-v5 margin-top-20">' +
                '<p class="fontsize-14">' + rstPubTim + '</p></div>');

            jQuery('.basicInfo').append('<tbody><tr>' +
                '<td width="16%">项目名称：</td><td colspan="2">' + resp.data.xjProj.projNam + '</td> ' +
                '<td width="16%">项目编号：</td> <td colspan="2">' + projNbr + '</td></tr><tr>' +
                '<td width="16%">物料品类：</td><td colspan="2">' + matTyp + '</td>' +
                '<td width="16%">报价次数类型：</td><td colspan="2">' + projqotCntTyp + '</td></tr> <tr>' +
                '<td width="16%">招标单位：</td><td colspan="2">' + resp.data.xjProj.bidDptNam + '</td>' +
                '<td width="16%">招标单位地址：</td><td colspan="2">' + resp.data.xjProj.bidDptAddr + '</td></tr><tr>' +
                '<td width="16%">是否需要公开招标价格：</td><td colspan="2">' + pubPriFlgFmt + '</td>' +
                // '<td width="16%">是否需要投标保证金：</td><td colspan="2">' + dpstFlg + '</td></tr>' +
                // '<tr><td width="16%">投标保证金（元）：</td><td colspan="2">' + resp.data.xjProj.bidDpst + '</td>' +
                '<td width="16%">币种：</td><td colspan="2">' + currTyp + '</td></tr><tr>' +
                '<td width="16%">联系人：</td><td colspan="2">' + resp.data.xjProj.ctct + '</td>' +
                '<td width="16%">联系电话：</td><td colspan="2">' + resp.data.xjProj.ctctTel + '</td></tr><tr>' +
                // '<tr><td width="16%">监标人：</td><td colspan="2">' + resp.data.xjProj.projSupv + '</td>' +
                // '<td width="16%">监标人联系电话：</td><td colspan="2">' + resp.data.xjProj.projSupvTel + '</td></tr><tr>' +
                '<td width="16%">投标开标时间：</td><td colspan="2">' + bidOpenTim + '</td>' +
                '<td width="16%">投标截止时间：</td><td colspan="2">' + bidEndTim + '</td></tr><tr>' +
                '<td>招标范围类型：</td><td colspan="2">' + bidRngTyp + '</td>' +
                '<td width="16%">指定招标供应商：</td><td colspan="2">' + splrs + '</td></tr><tr>' +
                '<td>备注信息：</td><td colspan="5">' + projMemo + '</td></tr></tbody>');

        },
        error: function (err) {
            alert('系统错误！');
        }
    });


    var bidResult = {
        init: function () {
            this.getResultData()
                .then(this.renderbidResult)
        },
        getResultData: function () {
            return new Promise((resolve, reject) => {
                let self = this;
                $.ajax({
                    url: HOST_ADDR + `/cpmp/xjProjMngForPortal/viewXjProjRstDtl/` + projId,
                    type: 'get',
                    dataType: 'json',
                    contentType: "application/json",
                    success: function (resp) {
                        if (resp.success) {
                            console.log(resp);
                            resolve(resp);
                        }
                        else {
                            alert(resp.msgInf);
                        }
                    },
                    error: function (err) {
                        alert(err);
                    }
                });
            });
        },
        // 渲染列表

        renderbidResult: function (resp) {
            let self = bidResult;
            // 处理数据begin
            var datalist = [];
            for(var i=0;i< resp.data.length; i++){
                var tmplist = resp.data[i];
                tmplist.dlvDteStr = '';
                tmplist.dlvAdr = '';
                tmplist.untPriFmt = '';
                tmplist.ttlPriFmt = '';
                if(pubPriFlg=='1'){
                    tmplist.untPriFmt = tmplist.untPri ? tmplist.untPri+"("+tmplist.currTyp+")" : '';
                    tmplist.ttlPriFmt = tmplist.ttlPri ? tmplist.ttlPri+"("+tmplist.currTyp+")" : '';
                }
                for(var m=0;m<matlist.length;m++){
                    if(tmplist.matId==matlist[m].id){
                        tmplist.dlvDteStr = matlist[m].dlvDte ? (new Date(matlist[m].dlvDte)).Format("yyyy-MM-dd") : "";
                        tmplist.dlvAdr = matlist[m].dlvAdr ? matlist[m].dlvAdr : "";
                    }
                }
                datalist.push(tmplist);
            }
            let resultHtml = projResultTpl({bidResult: datalist});
            $('.setInfo').append(resultHtml);
        },

        // 渲染列表
        renderList(){
            this.getResultData()
                .then(this.renderbidResult)
        }
    };
    if ((token != null && token != '')||(token_mng != null && token_mng != '')) {  // 登录后才可查看
        bidResult.init();
        $(".setInfo").css({"display": "block"});
    }
});